/*
 * 文件名：SubpackCotController.java
 * 版权：Copyright by www.infopub.top
 * 描述：
 * 修改人：wuzhiyang
 * 修改时间：2020年2月17日
 * 跟踪单号：
 * 修改单号：
 * 修改内容：
 */

package top.infopub.mgr.bpa.web;


import com.alibaba.fastjson.JSON;
import com.github.pagehelper.Page;
import org.apache.commons.lang3.StringEscapeUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import top.infopub.dto.bpa.DeviceEntryDto;
import top.infopub.dto.bpa.DeviceSuppleDto;
import top.infopub.dto.bpa.OtherContractsInventoryDto;
import top.infopub.mgr.bpa.domain.DeviceEntryDomain;
import top.infopub.mgr.bpa.service.DeviceEntryService;
import top.infopub.mgr.bpa.vo.DeviceEntryVo;
import top.infopub.mgr.common.page.ParamPage;
import top.infopub.mgr.common.vo.RspVo;
import top.infopub.mgr.flow.web.GenericFlowController;
import top.infopub.model.bpa.Basic;
import top.infopub.model.bpa.DeviceEntry;
import top.infopub.model.bpa.OtherContractsInventory;
import top.infopub.security.util.SessionUserUtils;
import top.infopub.security.vo.AuthorizationVO;

import javax.servlet.http.HttpServletRequest;
import java.text.ParseException;
import java.util.List;
import java.util.Map;


/**对内合同
 * @author wuzhiyang
 * @version 2020年2月17日
 * @see DeviceEntryController
 * @since 设备合同/合同清单
 */
@Controller
@RequestMapping("/deviceEntry")
public class DeviceEntryController extends GenericFlowController {

    @Autowired
    private DeviceEntryService deviceEntryService;

    /**
     * 设备合同清单
     * @return
     */
    @RequestMapping("/queryDataByCondition")
    @ResponseBody
    public RspVo<DeviceEntryDomain> queryDataByCondition(DeviceEntryVo deviceEntryVo,
                                                         ParamPage paramPage) throws ParseException {
        Page<DeviceEntryDomain> page = new Page<DeviceEntryDomain>();
        page.setPageNum(paramPage.getPage());
        page.setPageSize(paramPage.getLimit());
        page = deviceEntryService.selectByDomainWhere(page, deviceEntryVo);
        return RspVo.getSuccessResponse(page.getTotal(), page.getResult());
    }
    //下拉框数据
   //和补充协议是一样的

    //详情
    @RequestMapping("/queryById")
    @ResponseBody
    public DeviceEntryDto queryById(String id) {
        DeviceEntryDto dao =  deviceEntryService.queryById(id);
        return dao;
    }
    //下拉详情
    @RequestMapping("/projectBasic")
    @ResponseBody
    public Basic projectBasic(String proCode) {
        Basic basic =  deviceEntryService.projectBasic(proCode);
        return basic;
    }
    @RequestMapping("/toDealOrDetail")
    public String toDealOrDetail(HttpServletRequest request, String id) {
        request.setAttribute("id",id);
        return "bpa/lay-EquipmentContractList-detail";
    }
    // 保存提交
    @RequestMapping("/save")
    @ResponseBody
    public RspVo save(String bodyStr, String submitType, String proCode,String id) {
        if (StringUtils.isEmpty(bodyStr)) {
            return RspVo.getFailureResponse(0, "数据为空");
        }
        //登录人员信息
        AuthorizationVO vo = SessionUserUtils.getSessionAttributeForUserDtl();
        String object = StringEscapeUtils.unescapeJava(bodyStr);
        DeviceEntryDto dto = JSON.parseObject(object, DeviceEntryDto.class);
        if ("1".equals(submitType)) {//1是保存
            int i = deviceEntryService.save(dto);
            if (i > 0) {
                return RspVo.getSuccessResponse(0, "保存成功");
            } else {
                return RspVo.getFailureResponse(0, "保存失败");
            }
        } else { //2是提交

            //判断状态
            DeviceEntry deviceEntry  = deviceEntryService.findById(id);
            if (deviceEntry!=null)
            {
                String processStatus = deviceEntry.getProcessStatus();
                if ("1".equals(processStatus) || "9".equals(processStatus))
                {
                    return RspVo.getFailureResponse(0, "审批中或审批已完成，修改失败");
                }
            }

/*            if (StringUtils.isNotBlank(deviceEntry.getOaId())) {
                oaFlowService.finishOA(deviceEntry.getOaId());
            }*/
            //提交流程  先保存
            int i = deviceEntryService.save(dto);
            if (i > 0) {//保存成的情况 走流程
                //发起流程
                /*String process = proProcessManageService.sendProcess(ProProcessDetailTypeEnum.PRO_NODE_TYPE15, object, id, code, vo);*/
                return RspVo.getFailureResponse(0, "提交成功");
            } else { //提交-保存失败
                return RspVo.getFailureResponse(0, "提交失败");
            }
        }
    }
}
